package HotelRecommend;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HotelRecommendServlet extends HttpServlet {

    private static final long serialVersionUID = -2223843112214613877L;

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setHeader("content-type", "text/html;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("charset=utf-8");
        PrintWriter out = response.getWriter();
        /*
         * String[] distinct=request.getParameterValues("distinct");
         * System.out.println("distinct: "+distinct.length+distinct[0]); for(String s:distinct){ System.out.println(s);
         * }
         */
        String[] distinct = request.getParameterValues("distinct");
        List<String> distincts = Arrays.asList(distinct);
        String[] star = request.getParameterValues("star");
        List<String> stars = Arrays.asList(star);
        String[] label = request.getParameterValues("label");
        List<String> labels = Arrays.asList(label);
        String[] sort = request.getParameterValues("sort");
        System.out.println(sort.length);
        HotelSearch hotelsearch = new HotelSearch();
        hotelsearch.setDistrict(distincts);
        hotelsearch.setStar(stars);
        hotelsearch.setFeature(labels);
        hotelsearch.setSort(sort[0]);
        List<hotel> hotels = new TouristHotelRecommend(hotelsearch).doTouristHotelRecommed();
        System.out.println(hotels.size());
        for (hotel ho : hotels) {
            System.out.println(ho.getHotelname() + "  " + ho.getAdress() + "  " + ho.getDistrict());
        }
        request.setAttribute("hotels", hotels);
        request.getRequestDispatcher("hotel.jsp").forward(request, response);
        out.flush();
        out.close();
    }
}
